<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <h2>Phone信息</h2>
    <div>
      <button @click='handleGive'>赠送</button>
    </div>
    <h3>Flow</h3>
    <Flow></Flow>
    <h3>Bill</h3>
    <Bill></Bill>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
  <script type="text/javascript">
    const pub = new Vue()
    Vue.component('Flow', {
      data() {
        return {
          isValid: true
        }
      },
      template: `<div>
          <div>流量{{ isValid ? '可用' : '禁用' }}</div>
          <button v-if="isValid" @click='handleFlow'>使用流量</button>
        </div>`,
      methods: {
        handleFlow() {
          pub.$emit('flow-event')
        }
      },
      mounted() {
        pub.$on('bill-event', res => {
          this.isValid = res >= 0
        })
        pub.$on('give', () => {
          this.isValid = true
        })
      }
    })
    Vue.component('Bill', {
      data() {
        return {
          num: 10
        }
      },
      template: `
        <div>
          <div>话费:{{num}}</div>
          <div>
            <button @click='handleArrears'>一键欠费</button>
          </div>
        </div>
      `,
      methods: {
        // 欠费
        handleArrears() {
          this.num = -10
          pub.$emit('bill-event', this.num)
        }
      },
      mounted() {
        pub.$on('flow-event', () => {
          this.num--
        })
        pub.$on('give', () => {
          this.num = 20
        })
      }
    })
    const vm = new Vue({
      el: '#app',
      methods: {
        handleGive() {
          pub.$emit('give')
        }
      }
    });
  </script>
</body>

</html>